//
//  SendGiftView.m
//  linktuIOS
//
//  Created by 章为 on 16/5/30.
//  Copyright © 2016年 张伟. All rights reserved.
//

#import "SendGiftView.h"
#import "giftModel.h"
#import "UIButton+WebCache.h"
#import <POP.h>
#import "ShakeLabel.h"
#import "HPresentGiftModel.h"

#define  TransfromDuration  0.5
#define  ScaleDuration      0.4
#define  KeepDuration       1.5

@interface SendGiftView()<CAAnimationDelegate>
@property (weak, nonatomic) IBOutlet UIButton *iocnButton;
@property (weak, nonatomic) IBOutlet UILabel *nameLable;
@property (weak, nonatomic) IBOutlet UILabel *meoneyLable;
@property (weak, nonatomic) IBOutlet UIButton *giftButton;

@property (nonatomic,strong) NSTimer *gifttimer;
//@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) POPSpringAnimation *scaleanimation;
@property (nonatomic,assign) NSInteger checknumber;
@end

@implementation SendGiftView
-(void)awakeFromNib
{
//    self.numberLabel.hidden = YES;
    [super awakeFromNib];
    _numberLabel.font = [UIFont systemFontOfSize:30];
    _numberLabel.borderColor = [UIColor whiteColor];
    [_meoneyLable setTextColor:UIColorFromRGB(0xa6cd36)];
//    self.giftButton.backgroundColor = [UIColor redColor];
//    [_numberLabel setFont:[UIFont fontWithName:@"Helvetica-BoldOblique" size:30]];
}
+(instancetype)sendGiftView
{
    NSBundle *bud = [NSBundle mainBundle];
    NSArray *arry = [bud loadNibNamed:NSStringFromClass(self) owner:nil options:nil];
    SendGiftView *view = arry.lastObject;
    view.frame = CGRectMake(0, 0, 300, 80);
//    NSLog(@"ggggggggggggg%@",view.giftButton);
    return view;
}
-(NSMutableArray *)dataSource
{
    if(_dataSource == nil)
    {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

-(void)setModel:(LTMessageGiftBody *)model
{
    _model = model;
    NSString *pleace = @"";
    self.meoneyLable.text = [NSString stringWithFormat:NSLTLocalizedString(@"送出了一%@%@", nil),pleace,model.giftName];
    [self.giftButton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.giftImage] forState:UIControlStateNormal];

}
-(void)setUserinfo:(LTUser *)userinfo
{
    _userinfo = userinfo;
    self.nameLable.text = userinfo.name;
    [self.iocnButton sd_setBackgroundImageWithURL:[NSURL URLWithString:userinfo.avatar] forState:UIControlStateNormal placeholderImage:defaultImgae];

}
-(void)scaleAnmation
{
    self.numberLabel.hidden = NO;
    NSDictionary *temp = [self.dataSource firstObject];
    self.model = temp[@"giftModel"];
    self.userinfo = temp[@"userinfo"];
    self.numberLabel.text =  [NSString stringWithFormat:@" x%ld",self.model.number];
    
    CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"transform.scale"];//同上
    anima.fromValue = [NSNumber numberWithFloat:0.5f];
    anima.toValue = [NSNumber numberWithFloat:1.2f];
    anima.duration = .2f;
    anima.delegate = self;
    [self.numberLabel.layer addAnimation:anima forKey:@"scaleAnimation"];
}
-(NSMutableAttributedString *)strChangeAttrestrWithStr:(NSString *)str
{
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, 1)];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:36] range:NSMakeRange(1, str.length-1)];
    return attrStr;
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if(flag == YES)
    {
        [self.dataSource removeObjectAtIndex:0];
        if(self.dataSource.count != 0 )
        {
            [self scaleAnmation];
        }
        else{
            NSInteger  temp = arc4random() % 1000;
            self.checknumber = temp;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if(self.dataSource.count == 0 && self.checknumber == temp)
                {
                    [self  giftAnimationDone];
                }
            });
            
        }
    }

}

-(void)AnimationWithmodel:(LTMessageGiftBody *)model Anduserinfo:(LTUser *)userinfo number:(NSString *)numberstr
{
    NSDictionary *temp = @{@"giftModel":model,@"userinfo":userinfo,@"numberstr":numberstr};
    [self.dataSource addObject:temp];
    if(self.dataSource.count == 1)
    {
        [self  GiftAnimation];
    }
}

-(void)GiftAnimation
{
    self.hidden = NO;
    NSDictionary *temp = [self.dataSource firstObject];
    self.model = temp[@"giftModel"];
    self.userinfo = temp[@"userinfo"];
    self.numberLabel.text = [NSString stringWithFormat:@"x%ld",self.model.number];
//     self.numberLabel.attributedText = [self strChangeAttrestrWithStr: [NSString stringWithFormat:@" %@",temp[@"numberstr"]]];
    if(self.ishidden == YES)
        {
            self.ishidden = NO;
            dispatch_async(dispatch_get_main_queue(), ^{
                [UIView animateWithDuration:0.3 animations:^{
                    self.x = 10;
                } completion:^(BOOL finished) {
                    self.x = 10;
                    [self scaleAnmation];
                }];
            });
        }else
        {
            [self scaleAnmation];
        }
}

-(void)giftAnimationDone
{
    self.ishidden = YES;
    self.hidden   = YES;
    self.x        = -250;
}




-(void)translationAnimation
{
    self.x = - 300;
    self.moveBlock(self,Transfroming);
    [UIView animateWithDuration:TransfromDuration animations:^{
        self.x = 10;
    } completion:^(BOOL finished) {
        self.x = 10;
        if(self.moveBlock)
        {
            self.moveBlock(self,TransfromEnd);
        }
        [self ScaleNumberlable];
    }];
}

-(void)ScaleNumberlable
{
    self.moveBlock(self,Scaleing);
    self.numberLabel.transform = CGAffineTransformMakeScale(0.3, 0.3);
    [UIView animateWithDuration:ScaleDuration delay:0 usingSpringWithDamping:3 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.numberLabel.transform = CGAffineTransformMakeScale(1, 1);
    } completion:^(BOOL finished) {
        if(self.moveBlock)
        {
            BOOL isKeep  = self.moveBlock(self,ScaleEnd);
            if(isKeep)
            {
                [self Keep];
            }
        }
    }];
}


-(void)Keep
{
    self.x = 10;
    self.moveBlock(self,Keeping);
    [UIView animateWithDuration:KeepDuration animations:^{
        self.x = 10.01;
    } completion:^(BOOL finished) {
        if(finished){
            if(self.moveBlock)
            {
                if(self.moveBlock(self,KeepEnd))
                {
                    self.x = 10.01;
                }else
                {
                    self.x = - 300;
                }
            }
        }
    }];
}



-(HPresentGiftModel *)nextgiftmessage
{
    HPresentGiftModel *model = nil;
    if(self.dataSource.count > 0){
        model  = self.dataSource.firstObject;
        [self.dataSource removeObjectAtIndex:0];
    }
    
    return model;
}
-(void)setUserID:(NSString *)userID
{
    _userID = userID;
    self.numberLabel.text = [NSString stringWithFormat:@" %@",userID];
}


//-(void)setPresentModel:(HPresentGiftModel *)presentModel
//{
//    _presentModel = presentModel;
//    dispatch_async(dispatch_get_main_queue(), ^{
//        self.numberLabel.text = [NSString stringWithFormat:@" %@",presentModel.number];
//        self.model = presentModel.giftmodel;
//        self.userinfo = presentModel.userinfo;
//    });
//
//
//
//}
@end
